﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace CuaHangBangDia.Home
{
    public partial class MainFrom : DevComponents.DotNetBar.Office2007RibbonForm
    {
        public MainFrom()
        {
            InitializeComponent();
        }

        private void MainFrom_Load(object sender, EventArgs e)
        {
            //DangNhapHT.DangNhapForm dn = new DangNhapHT.DangNhapForm();
            //dn.Show();
            timer1.Enabled = true;
            timer1.Start();

            
        }

        private void btniThemBD_Click(object sender, EventArgs e)
        {
            pnMain.Controls.Clear();
            QLBD.ThemBDForm themForm = new QLBD.ThemBDForm();
            themForm.TopLevel = false;
            themForm.FormBorderStyle = FormBorderStyle.None;
            themForm.Dock = DockStyle.Fill;
            pnMain.Controls.Add(themForm);
            themForm.Show();
        }

        private void rbbCapNhatBD_ItemClick(object sender, EventArgs e)
        {
            pnMain.Controls.Clear();
            QLBD.QLBDForm qlbdForm = new QLBD.QLBDForm();
            qlbdForm.TopLevel = false;
            qlbdForm.FormBorderStyle = FormBorderStyle.None;
            qlbdForm.Dock = DockStyle.Fill;
            pnMain.Controls.Add(qlbdForm);
            qlbdForm.Show();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            sttGio.Text = DateTime.Now.ToLongTimeString();
            sttNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");
        }

        private void btniBackup_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "SQL file .bak|*.bak";
            saveFileDialog1.Title = "Save an SQL file";
            saveFileDialog1.ShowDialog();
            string chuoi = saveFileDialog1.FileName;
            if (!chuoi.Equals(""))
            {
                try
                {

                    string sqlBackup = "BACKUP DATABASE [ThuVien] TO DISK='" + chuoi + "'";
                    SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=ThuVien;Integrated Security=True");
                    con.Open();
                    SqlCommand cmd = new SqlCommand(sqlBackup, con);
                    cmd.ExecuteNonQuery();
                    con.Close();
                    MessageBox.Show("Đã backup cơ sở dữ liệu");
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message, "Backup Database");
                    return;
                }
                catch (Exception)
                {

                }
            }

        }

        private void btniRestore_Click(object sender, EventArgs e)
        {
            try
            {
                openFileDialog1.Filter = "SQL file |*.bak";
                openFileDialog1.Title = "Chọn file Restore .bak";
                openFileDialog1.FileName = "";
                openFileDialog1.ShowDialog();
                string path = openFileDialog1.FileName;
                if (!path.Equals(""))
                {
                    string sqlRestore = "Use master Restore Database [KetNoiMoi] from disk='" + path + "'";
                    SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=KetNoiMoi;Integrated Security=True");
                    con.Open();
                    SqlCommand cmd = new SqlCommand(sqlRestore, con);
                    cmd.ExecuteNonQuery();
                    con.Close();
                    MessageBox.Show("Database da duoc restore ");

                }

            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message, "Restore Database");
                return;
            }
            catch (Exception)
            {

            }
        }

        private void MainFrom_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void btniNhapHang_Click(object sender, EventArgs e)
        {
            pnMain.Controls.Clear();
            DoanhThu.ThemNhapHangForm tnhForm = new DoanhThu.ThemNhapHangForm();
            tnhForm.TopLevel = false;
            tnhForm.FormBorderStyle = FormBorderStyle.None;
            tnhForm.Dock = DockStyle.Fill;
            pnMain.Controls.Add(tnhForm);
            tnhForm.Show();
        }

        private void btniCNNhapHang_Click(object sender, EventArgs e)
        {
            pnMain.Controls.Clear();
            DoanhThu.CapNhatNhapHangForm cnnhForm = new DoanhThu.CapNhatNhapHangForm();
            cnnhForm.TopLevel = false;
            cnnhForm.FormBorderStyle = FormBorderStyle.None;
            cnnhForm.Dock = DockStyle.Fill;
            pnMain.Controls.Add(cnnhForm);
            cnnhForm.Show();
        }

        private void btniCapNhatBH_Click(object sender, EventArgs e)
        {
            pnMain.Controls.Clear();
            DoanhThu.CapNhatBanHangForm cnbhForm = new DoanhThu.CapNhatBanHangForm();
            cnbhForm.TopLevel = false;
            cnbhForm.FormBorderStyle = FormBorderStyle.None;
            cnbhForm.Dock = DockStyle.Fill;
            pnMain.Controls.Add(cnbhForm);
            cnbhForm.Show();
        }

        private void btniDoanhThu_Click(object sender, EventArgs e)
        {
            pnMain.Controls.Clear();
            DoanhThu.CapNhatDTTheoNgayForm cndt = new DoanhThu.CapNhatDTTheoNgayForm();
            cndt.TopLevel = false;
            cndt.FormBorderStyle = FormBorderStyle.None;
            cndt.Dock = DockStyle.Fill;
            pnMain.Controls.Add(cndt);
            cndt.Show();
        }

        private void btnlBD_Click(object sender, EventArgs e)
        {
            rbbTabBangDia.Select();
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            rbbTabDoanhThu.Select();
        }      
    }
}
